import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import RouterGenerator from '../utils/RouterGenerator'
Vue.use(Router)
let dynamic = [];
let menus = store.state.account.menus;
RouterGenerator(dynamic, menus);
const router =  new Router({
    mode: 'history',
    linkActiveClass: 'active',
    routes: [
        {
          path: '/admin',
          component: () => import('../views/Admin.vue'),
          redirect: '/admin/member',
          meta: {
            title: '控制台',
            requiresAuth: true
          },
          children: [
            ...dynamic
          ]
        },
        {
          path: '/login',
          component: () => import('../views/login.vue')
        },
        {
          path: '/',
          beforeEnter: (to, from, next) => {
            /**
             * 验证用户是否登录
             */
            if (store.state.account.access_token) {
              router.replace({
                path: '/admin/member'
              })
            } else {
              router.replace({
                path: '/login'
              })
            }
          }
        },
        {
            path:'*',
            component: () => import('../views/errors/NotFound.vue'),
        }
    ]
})

export default router;
